package fr.esiag.isidis.epp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class Utils {

	private static Logger logger = Logger.getLogger(Utils.class);
	
	public static int convertDateToInt(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int year       = calendar.get(Calendar.YEAR);
		int month      = calendar.get(Calendar.MONTH) + 1; // jan = 0 +1 = 1
		int day = calendar.get(Calendar.DAY_OF_MONTH); 
		
		//ex : 20141021 = date 21/10/2014
		return year*10000+month*100+day;
		
	}
	
	public static int convertDateToIntSite(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int year       = calendar.get(Calendar.YEAR);
		int month      = calendar.get(Calendar.MONTH) + 1; // jan = 0 +1 = 1
		int day = calendar.get(Calendar.DAY_OF_MONTH); 
		
		//ex : 20141021 = date 21/10/2014
		return year*10000+month*100+day;
		
	}
	
	
	public static int convertHourToInt(String date){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		try {
			calendar.setTime(format.parse(date));
		} catch (ParseException e) {
			logger.error("date format does not conform");
		}

		int hour  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
		int minute     = calendar.get(Calendar.MINUTE);
	//	int second     = calendar.get(Calendar.SECOND);

		//ex : 1210 => 20h 10mn 00s
		return hour*60+minute;
	}
	
	public static String getIdnn(int id){
		id = id+1;
		if (id == 1) return ("ID01");
		if (id == 2) return ("ID02");
		if (id == 3) return ("ID03");
		if (id == 4) return ("ID04");
		if (id == 5) return ("ID05");
		if (id == 6) return ("ID06");
		if (id == 7) return ("ID07");
		if (id == 8) return ("ID08");
		if (id == 9) return ("ID09");
		else return ("ID"+id);
	}
	
	// methode pour tranformer id01 en idsite1
	public static String transformeIdnn(String id){
		String res;
		String sufix = id.substring(2, id.length());
		if(sufix.equals("01")){res = "IDSITE1"; return res;}
		if(sufix.equals("02")){res = "IDSITE2"; return res;}
		if(sufix.equals("03")){ res = "IDSITE3"; return res;}
		if(sufix.equals("04")){ res = "IDSITE4"; return res;}
		if(sufix.equals("05")){res = "IDSITE5"; return res;}
		if(sufix.equals("06")){res = "IDSITE6"; return res;}
		if(sufix.equals("07")){res = "IDSITE7"; return res;}
		if(sufix.equals("08")){res = "IDSITE8"; return res;}
		if(sufix.equals("09")){res = "IDSITE9"; return res;}
		else
		res = id.replace("ID", "IDSITE");
		return res;
		
	}
	
	public static String virguleTopoint(String a){
		if(a.contains(",")){
		String[] temp = a.split(",");
		a = temp[0]+"."+temp[1];
		return a;
		}else
			return a;
	}
	
//    public static void main( String[] args ){
//    	System.out.println("entre un signe");
//    	Scanner sc = new Scanner(System.in);
//    	String a = sc.nextLine();
//    	String b = a.substring(2, a.length());
//    	a = Utils.transformeIdnn(a);
//    	System.out.println(a);
//    	System.out.println(b);
//    }
}
